<template>
  <view class="uni-container">
    <uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast" labelPosition="top" labelWidth="200">
      <uni-forms-item name="post_name" label="房屋名称" required>
        <uni-easyinput placeholder="房屋名称" v-model="formData.post_name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="amount" label="月数" required>
        <uni-easyinput placeholder="月数" type="number" v-model="formData.amount"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="user_phone" label="租客联系电话" required>
        <uni-easyinput placeholder="租客联系电话" v-model="formData.user_phone"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="user_name" label="租客姓名" required>
        <uni-easyinput placeholder="租客姓名" v-model="formData.user_name"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="price" label="总价">
        <uni-easyinput placeholder="总价" type="number" v-model="formData.price"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="deposit" label="押金" required>
        <uni-easyinput placeholder="押金" type="number" v-model="formData.deposit"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="rent_start" label="租房日期" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.rent_start"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="rent_end" label="结束日期" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.rent_end"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="next_charge_time" label="下次缴费日期" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.next_charge_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="price_type" label="交租类型">
        <uni-data-checkbox v-model="formData.price_type" :localdata="formOptions.price_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="contract" label="合同">
        <uni-file-picker return-type="object" v-model="formData.contract"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="remark" label="备注">
        <uni-easyinput placeholder="备注" v-model="formData.remark"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="status" label="订单状态">
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="user_idcard" label="租客身份证号">
        <uni-easyinput placeholder="租客身份证号" v-model="formData.user_idcard"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="idcard_url" label="身份证照片">
        <uni-file-picker return-type="object" v-model="formData.idcard_url"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="price_per_month" label="月租金">
        <uni-easyinput placeholder="月租金" v-model="formData.price_per_month"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="net_fee" label="网费">
        <uni-easyinput placeholder="网费" v-model="formData.net_fee"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="water_fee" label="水费">
        <uni-easyinput placeholder="水费" v-model="formData.water_fee"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="fire_fee" label="燃气费">
        <uni-easyinput placeholder="燃气费" v-model="formData.fire_fee"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/t_order.js';

  const db = uniCloud.database();
  const dbCollectionName = 't_order';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.indexOf(key) > -1) {
        result[key] = validator[key]
      }
    }
    return result
  }

  export default {
    data() {
      let formData = {
        "post_name": "",
        "amount": null,
        "user_phone": "",
        "user_name": "",
        "price": null,
        "deposit": null,
        "rent_start": null,
        "rent_end": null,
        "next_charge_time": null,
        "price_type": 1,
        "contract": null,
        "remark": "",
        "status": 0,
        "user_idcard": "",
        "idcard_url": null,
        "price_per_month": "",
        "net_fee": "",
        "water_fee": "",
        "fire_fee": ""
      }
      return {
        formData,
        formOptions: {
          "price_type_localdata": [
            {
              "text": "月付",
              "value": 1
            },
            {
              "text": "季付",
              "value": 2
            },
            {
              "text": "半年付",
              "value": 3
            },
            {
              "text": "年付",
              "value": 4
            }
          ],
          "status_localdata": [
            {
              "text": "进行中",
              "value": 0
            },
            {
              "text": "已结束",
              "value": 1
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>

<style>
  .uni-container {
    padding: 15px;
  }

  .uni-input-border,
  .uni-textarea-border {
    width: 100%;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    box-sizing: border-box;
  }

  .uni-input-border {
    padding: 0 10px;
    height: 35px;

  }

  .uni-textarea-border {
    padding: 10px;
    height: 80px;
  }

  .uni-button-group {
    margin-top: 50px;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    justify-content: center;
  }

  .uni-button {
    width: 184px;
    padding: 12px 20px;
    font-size: 14px;
    border-radius: 4px;
    line-height: 1;
    margin: 0;
  }
</style>
